<?php
/******************************************************************
*Description:数据库操作类
******************************************************************/
define('WORD_ROOT_PATH', dirname(dirname(__FILE__)));
include_once( WORD_ROOT_PATH . "/db/config.php" );

class DBMySQL
{
	private $dbServer;
	private $dbUser;
	private $dbPSW;
	private $dbName;
	
	function DBMySQL()
	{
		global $db;
		$this->dbServer = $db['server'];	
		$this->dbUser = $db['user'];
		$this->dbPSW = $db['pass'];
		$this->dbName = $db['db'];
	}
	
	public function Query1( $sql )
	{
		$result = $this->DBQuery($sql);
		return $result;
	}

	public function Query ( $sql )
	{
		$rs = $this->DBQuery($sql);
		if( !empty($rs) ){
			while($row = $rs->fetch_array() ){
				$result[] = $row;
			}
			return $result;
		}
		return -1;
	}

	public function ExecuteSQL( $sql )
	{
		$result = $this->DBQuery($sql);
		return $result;
	}

	public function Insert( $sql )
	{
		$mysqli = $this->ConnectDB();
		$mysqli->real_query($sql);
		$insertID = $mysqli->insert_id;
		$mysqli->commit();
		$mysqli->close();
		return $insertID;
	}

	public function Modify( $sql )
	{
		$mysqli = $this->ConnectDB();
		$rs = $mysqli->real_query($sql);
		$UpdateID = $mysqli->affected_rows;
		$mysqli->commit();
		$mysqli->close();
		return $UpdateID;
	}

	public function ConnectDB( )
	{
		$mysqli = new mysqli();
		$mysqli->connect( $this->dbServer, $this->dbUser, $this->dbPSW, $this->dbName);
		$mysqli->set_charset("utf8") ;
		return $mysqli;
	}

	private function DBQuery( $sql )
	{

		$mysqli = $this->ConnectDB();
		$mysqli->real_query($sql);
		$result = $mysqli->store_result();
		$mysqli->close();
		return $result;
	}

	public  function ChangeDBServer( $server, $db, $user, $psw )
	{
		$this->dbServer = $server;
		$this->dbName = $db;
		$this->dbUser = $user;
		$this->dbPSW = $psw;
	}

}
?>